#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
typedef struct phone {
  int num;
  char name[20];
  struct phone *next;
  struct phone *pre;
} PH;
PH *readfile(PH *head);
PH *sort_add(PH *head, PH *load);
void dealloc_all_nodes(PH *head);
void input_node(PH *load);
PH *add_new_phone(PH *head);
void show_all_nodes(PH *head);
PH *delete_one_node(PH *head);
PH *search_one_node(PH *head, char *string);
int menu_show(void) {
  int chose;
  printf("         1.add a new phone\n");
  printf("         2.modify a phone\n");
  printf("         3.search a  phone\n");
  printf("         4.delete a  phone\n");
  printf("         5.show all  phones\n");
  printf("         6.exit and free  momery\n");
  printf("         \nplease input your function chose\n");
  scanf("%d", &chose);
  return chose;
}
PH *menu_function(PH *head, int chose) {
  char string[20];
  PH *load = NULL;
  switch (chose) {
    case 1:
      head = add_new_phone(head);
      break;
    case 2:
      printf("please input the name modified\n");
      scanf("%s", string);
      load = search_one_node(head, string);
      input_node(load);
      break;
    case 3:
      printf("please input the name searched\n");
      scanf("%s", string);
      load = search_one_node(head, string);
      printf("        %d %s \n", load->num, load->name);
      break;
    case 4:
      head = delete_one_node(head);
      break;
    case 5:
      show_all_nodes(head);
      break;
    case 6:
      dealloc_all_nodes(head);
      exit(0);
      break;
  }
  return head;
}
void dealloc_all_nodes(PH *head) {
  PH *load = head;
  int retur = 0;
  long long pos;
  FILE *fp = fopen("phone.txt", "wb+");
  while (load != NULL) {
    head = load->next;
    retur = fwrite(load, sizeof(PH), 1, fp);
    pos = ftell(fp);
    printf("****    %d\n", pos / sizeof(PH));
    free(load);
    load = head;
  }
  fclose(fp);
}
void input_node(PH *load) {
  printf("please input your number:\n");
  scanf("%d", &load->num);
  printf("please input your name:\n");
  scanf("%s", load->name);
}
PH *sort_add(PH *head, PH *load) {
  PH *st = head;
  if (head == NULL) {
    load->pre = NULL;
    load->next = NULL;
    head = load;
  }
  for (; st != NULL; st = st->next) {
    if (strcmp(load->name, st->name) < 0 || strcmp(load->name, st->name) == 0) {
      if (st->pre == NULL) {
        load->next = st;
        st->pre = load;
        load->pre = NULL;
        head = load;
      } else {
        load->pre = st->next;
        st->pre = load;
        st->pre->next = load;
        load->next = st;
      }
      break;
    } else if (st->next == NULL) {
      load->pre = st;
      st->next = load;
      load->next = NULL;
      break;
    }
  }
  return head;
}
PH *add_new_phone(PH *head) {
  PH *load = (PH*)malloc(sizeof(PH));
  assert(load != NULL);
  input_node(load);
  //head=head_add(head,load);
  head = sort_add(head, load);
  return head;
}
PH *delete_one_node(PH *head) {
  char string[20];
  printf("what do you want to delete:\n");
  scanf("%s", string);
  PH *load = NULL;
  load = search_one_node(head, string);
  if (load == head) {
    load->next->pre = NULL;
    head = head->next;
    free(load);
  } else if (load->next == NULL) {
    load->pre->next = NULL;
    free(load);
  } else {
    load->next->pre = load->pre;
    load->pre->next = load->next;
    free(load);
  }
  return head;
}
PH *search_one_node(PH *head, char *string) {
  PH *load = head;
  while (load != NULL) {
    if (strcmp(load->name, string) == 0) {
      return load;
    }
    load = load->next;
  }
  return NULL;
}
void show_all_nodes(PH *head) {
  PH *load = head;
  while (load != NULL) {
    printf("          %d %s \n", load->num, load->name);
    load = load->next;
  }
}
PH *readfile(PH *head) {
  int retur = 0;
  FILE *fp = fopen("phone.txt", "rb");
  assert(fp != NULL);
  PH *load = malloc(sizeof(PH));
  assert(load != NULL);
  while ((retur = fread(load, sizeof(PH), 1, fp)) == 1) {
    head = sort_add(head, load);
    load = malloc(sizeof(PH));
    assert(load != NULL);
  }
  if (retur == 0) {
    free(load);
  }
  fclose(fp);
  return head;
}
int main() {
  int chose;
  PH *head = NULL;
  head = readfile(head);
  while (1) {
    chose = menu_show();
    head = menu_function(head, chose);
  }
  return 1;
}
